﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using KaoQin.Model;
using KaoQin.BLL;

namespace KaoQin.WPFApp.sys.User
{
    /// <summary>
    /// SysUserAdd.xaml 的交互逻辑
    /// </summary>
    public partial class SysUserAdd : Window
    {
        public SysUserAdd()
        {
            InitializeComponent();
        }

        private string userId = string.Empty;

        public SysUserAdd(string id)
        {
            InitializeComponent();
            SysUsers bll = new SysUsers();

            userId = id;

            SysUsersInfo info = bll.GetModel(id);
            if (info != null)
            {
                tbKName.Text = info.KName.ToString();
                tbPassword1.Text = info.Password;
                tbRealName.Text = info.RealName;
                tbMobilePhone.Text = info.MobilePhone;
                rdoState2.IsChecked = info.KState == 0 ? true : false;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SysUsersInfo model = new SysUsersInfo();
            model.KName = tbKName.Text.Trim();
            model.Password = tbPassword1.Text.Trim();
            model.RealName = tbRealName.Text.Trim();
            model.MobilePhone = tbMobilePhone.Text.Trim();
            model.KState = rdoState1.IsChecked == true ? 1 : 0;

            if (string.IsNullOrEmpty(userId))
            {
                model.ID = Guid.NewGuid().ToString();
                new SysUsers().Add(model);
            }
            else
            {
                model.ID = userId;
                new SysUsers().Update(model);
            }
            MessageBox.Show("保存成功");
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
